package com.freemaster.sys.service.impl;

import java.util.Collections;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.freemaster.sys.entity.User;
import com.freemaster.sys.mapper.UserMapper;
import com.freemaster.sys.service.RoleService;
import com.freemaster.sys.service.UserService;


@Service
@Transactional(readOnly =false)
public class UserServiceImpl implements UserService {

	
	@Autowired
	UserMapper userMapper;

	@Autowired
	RoleService roleService;
	
	@Override
	public Set<String> findRoles(String username) {
		User user = findByUsername(username);
		if(user ==null){
			return Collections.EMPTY_SET;
		}
		return roleService.findRoles(user.getRoleIds().toArray(new Long[0]));
	}

	@Override
	public User findByUsername(String username) {
		// TODO Auto-generated method stub
		return userMapper.findByUsername(username);
	}

	/**
	 * 根据用户名查找其权限
	 */
	@Override
	public Set<String> findPermissions(String username) {
		User user = findByUsername(username);
		if(user ==null){
			return Collections.EMPTY_SET;
		}
		
		return roleService.findPermissions(user.getRoleIds().toArray(new Long[0]));
	}

 
	
	
}
